package com.qy139.card.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qy139.card.mapper.CardInfoMapper;
import com.qy139.card.service.ICardInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qy139.card.util.RandomUtil;
import com.qy139.entity.CardInfo;
import com.qy139.util.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.smartcardio.Card;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yuhaochen
 * @since 2021-11-30
 */
@Service
public class CardInfoServiceImpl extends ServiceImpl<CardInfoMapper, CardInfo> implements ICardInfoService {
    @Resource
    private CardInfoMapper cardInfoMapper;

    @Override
    public IPage<CardInfo> selectALl(Page<CardInfo> page, Integer cardnum) {
        return cardInfoMapper.selectALl(page,cardnum);
    }

    @Transactional
    @Override
    public Result addCard(CardInfo cardInfo) {
        Calendar cal = new GregorianCalendar();
        String cardnum = RandomUtil.toDateString(cal);
        cardInfo.setCardnum(cardnum);
        int i = cardInfoMapper.insert(cardInfo);
        if (i > 0){
            return new Result(cardnum);
        }
        return new Result(400,"error","");
    }
}
